using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using XF.FM.Core.Table;
using XF.FM.TagHelpers.Exts;

namespace XF.FM.TagHelpers
{
    public class PageTagHelper : TagHelper
    {
        public PageData AspFor { get; set; }
        private int pageLength;
        public override void Init(TagHelperContext context)
        {
            //base.Init(context);
            if (AspFor.Total == 0)
                pageLength = 1;
            else
                pageLength = (int)Math.Ceiling((double)AspFor.Total / AspFor.Length);
        }
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (AspFor == null)
                throw new Exception("分页数据不能为null");
            output.TagName = "ul";
            //pagination m-0 float-right
            output.AddClass("pagination m-0 float-right");

            //上一页
            TagBuilder pre = new TagBuilder("li");
            if (AspFor.Index == 0)
                pre.AddCssClass("page-item disabled");
            else
                pre.AddCssClass("page-item");
            pre.InnerHtml.AppendHtml(CreateA("上一页", AspFor.Index -1));
            output.Content.AppendHtml(pre);

            Nbs(output);

            //下一页
            TagBuilder next = new TagBuilder("li");
            if (AspFor.Index == pageLength - 1)
                next.AddCssClass("page-item disabled");
            else
                next.AddCssClass("page-item");
            next.InnerHtml.AppendHtml(CreateA("下一页", AspFor.Index + 1));

            output.Content.AppendHtml(next);

            return Task.CompletedTask;
        }
        private void Nbs(TagHelperOutput output)
        {
            if (pageLength < 7)
            {
                if (pageLength == 1)
                {
                    var li = CreateLi(( 1).ToString(), 0, 0 == AspFor.Index, false);
                    output.Content.AppendHtml(li);
                }
                else
                {
                    for (int i = 0; i < pageLength; i++)
                    {
                        var li = CreateLi((i + 1).ToString(), i, i == AspFor.Index, false);
                        output.Content.AppendHtml(li);
                    }
                }
            }
            else
            {
                output.Content.AppendHtml(CreateLi("1", 0, 0 == AspFor.Index, false));
                if (AspFor.Index <= 3)
                {
                    for (int i = 1; i <= 5; i++)
                    {
                        bool disable = i == 5;
                        var li = CreateLi(disable ? "..." : (i + 1).ToString(), i, i == AspFor.Index, disable);
                        output.Content.AppendHtml(li);
                    }
                }
                else if (AspFor.Index > pageLength - 1 - 4)
                {
                    for (int i = pageLength - 2 - 4; i <= pageLength - 2; i++)
                    {
                        bool disable = i == pageLength - 2 - 4;
                        var li = CreateLi(disable ? "..." : (i + 1).ToString(), i, i == AspFor.Index, disable);
                        output.Content.AppendHtml(li);
                    }
                }
                else
                {
                    for (int i = AspFor.Index - 2; i <= AspFor.Index + 2; i++)
                    {
                        bool disable = i == AspFor.Index - 2 || i == AspFor.Index + 2;
                        var li = CreateLi(disable ? "..." : (i + 1).ToString(), i, i == AspFor.Index, disable);
                        output.Content.AppendHtml(li);
                    }
                }
                output.Content.AppendHtml(CreateLi(pageLength.ToString(), pageLength - 1, pageLength - 1 == AspFor.Index, false));
            }
        }
        private TagBuilder CreateLi(string title, int pageIndex, bool active, bool disable)
        {
            TagBuilder li = new TagBuilder("li");
            li.AddCssClass("page-item");
            if (disable)
                li.AddCssClass("disabled");
            if (active)
                li.AddCssClass("active");
            li.InnerHtml.AppendHtml(CreateA(title, pageIndex));
            return li;
        }
        private TagBuilder CreateA(string title, int pageIndex)
        {
            TagBuilder a = new TagBuilder("a");
            a.AddCssClass("page-link");
            a.Attributes.Add("href", "javascript:;");
            a.Attributes.Add("data-pg-i", pageIndex.ToString());
            a.Attributes.Add("data-pg-n","start");
            a.InnerHtml.SetContent(title);

            return a;
        }
    }
}
